package com.example.fresheveryday.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.fresheveryday.po.Comment;
import com.example.fresheveryday.restful.JsonResult;
import com.example.fresheveryday.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ll
 * @since 2023-07-08
 */
@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    CommentService commentService;
    @RequestMapping("/addOrUpdateComment")
    public JsonResult saveOrUpdate(@RequestBody Map<String,Object> map){
        System.out.println(map);
        String userId = map.get("userId").toString();
        String rate = map.get("rate").toString();
        String productId = map.get("productId").toString();
        String content = map.get("content").toString();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("新增评论" + map);
        Comment comment = new Comment();
        comment.setContent(content);
        comment.setUserId(userId);
        comment.setRate(rate);
        comment.setCreateDate(new Date());
        comment.setProductId(productId);
        commentService.saveOrUpdate(comment);
        return JsonResult.success("success");
    }
    @RequestMapping("/getCommentByProductId")
    public IPage<Comment> getById(@RequestBody Map map){
        System.out.println(map);
        String productId = map.get("productId").toString();
        System.out.println("前端商品对应评论" + productId);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("product_id",productId);
        Page<Comment> page = new Page<>();

        page.setCurrent(Integer.valueOf(map.get("pageNo").toString()))
                .setTotal(commentService.countByConditions(wrapper))
                .setSize(Integer.valueOf(map.get("pageSize").toString()));

        return commentService.getCommentByConditions(page,wrapper);
    }
    @RequestMapping("/getCommentByUserId")
    public IPage<Comment> getByUserId(@RequestBody Map map){

        String userId = map.get("userId").toString();
        System.out.println("前端用户对应评论" + userId);
        QueryWrapper wrapper = new QueryWrapper();
        if(map.get("userId") != "") {

            wrapper.eq("user_id", userId);
        }
        Page<Comment> page = new Page<>();

        page.setCurrent(Integer.valueOf(map.get("pageNo").toString()))
                .setTotal(commentService.countByConditions(wrapper))
                .setSize(Integer.valueOf(map.get("pageSize").toString()));

        return commentService.getCommentByConditions(page,wrapper);
    }

    @RequestMapping("/getAll")
    public IPage<Comment> getAll(@RequestBody Map map){
        Page<Comment> page = new Page<>();
        page.setCurrent(Integer.valueOf(map.get("pageNo").toString()))
                .setTotal(commentService.count())
                .setSize(Integer.valueOf(map.get("pageSize").toString()));
//        System.out.println(page);
        return commentService.getAllComment(page);
    }
}
